package com.ytc.teaching.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ytc.commons.model.dto.CommentCourseDto;
import com.ytc.commons.model.dto.CommentQueryDto;
import com.ytc.commons.model.dto.PageRequest;
import com.ytc.commons.model.vo.*;
import com.ytc.commons.utils.AuthInfoHolder;
import com.ytc.commons.utils.BeanHelper;
import com.ytc.content.api.ContentClient;
import com.ytc.system.api.SystemClient;
import com.ytc.teaching.domain.Comment;
import com.ytc.teaching.domain.CommentReply;
import com.ytc.teaching.mappers.CommentMapper;
import com.ytc.teaching.service.CommentReplyService;
import com.ytc.teaching.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiuye
 * @since
 */
@Service
@SuppressWarnings("all")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentReplyService commentReplyService;
    @Autowired
    private  SystemClient systemClient;
    @Autowired
    private ContentClient contentClient;

    /**
     * 查询教育机构的评论列表
     *
     * @param
     * @return
     */
    @Override
    public ResponseResult commentList(Integer pageNo, Integer pageSize, CommentQueryDto dto) {
        Page<Comment> page = new Page(pageNo, pageSize);
        //组装条件
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        //设置条件
        qw.eq(dto.getLevel() != null, Comment::getStarRank, dto.getLevel())//评价等级
                .eq(!StringUtils.isEmpty(dto.getReplyStatus()), Comment::getReplyStatus, dto.getReplyStatus())//评价状态
                .eq(dto.getTargetId() != null, Comment::getTargetId, dto.getTargetId())
                .eq(!StringUtils.isEmpty(dto.getTargetName()), Comment::getTargetName, dto.getTargetName());
        Page<Comment> commentPage = this.page(page, qw);
        //循环设置课程回复表
        List<CommentVo> commentVos = BeanHelper.copyWithCollection(commentPage.getRecords(), CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            //查询评论回复表
            LambdaQueryWrapper<CommentReply> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(CommentReply::getCommentId, commentVo.getCommentId());
            List<CommentReply> commentReplyList = commentReplyService.list(qw2);
            List<CommentReplyVo> commentReplyVoList = BeanHelper.copyWithCollection(commentReplyList, CommentReplyVo.class);
            commentVo.setReplyDTOList(commentReplyVoList);
        }
        return PageResponseResult.okResult( commentPage.getTotal(), commentVos);
    }

    /**
     * 运营平台---机构的评论列表
     *
     * @param
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult listAll(CommentQueryDto dto, Integer pageNo, Integer pageSize) {
        Page<Comment> page = new Page(pageNo, pageSize);
        //组装条件
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        //设置条件
        qw.eq(dto.getLevel() != null, Comment::getStarRank, dto.getLevel())//评价等级
                .eq(!StringUtils.isEmpty(dto.getReplyStatus()), Comment::getReplyStatus, dto.getReplyStatus())//评价状态
                .eq(dto.getTargetId() != null, Comment::getTargetId, dto.getTargetId())
                .eq(!StringUtils.isEmpty(dto.getTargetName()), Comment::getTargetName, dto.getTargetName());
        Page<Comment> commentPage = this.page(page, qw);
        //循环设置课程回复表
        List<CommentVo> commentVos = BeanHelper.copyWithCollection(commentPage.getRecords(), CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            //查询评论回复表
            LambdaQueryWrapper<CommentReply> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(CommentReply::getCommentId, commentVo.getCommentId());
            List<CommentReply> commentReplyList = commentReplyService.list(qw2);
            List<CommentReplyVo> commentReplyVoList = BeanHelper.copyWithCollection(commentReplyList, CommentReplyVo.class);
            commentVo.setReplyDTOList(commentReplyVoList);
        }
        return new PageResponseResult(commentPage.getTotal(), commentVos);
    }

    /**
     * 门户详情页-评论课程--新增
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult courseComment(CommentCourseDto dto) {
        User user = systemClient.findById(AuthInfoHolder.getUserId()).getData();
        CourseVo courseVo = contentClient.findByCourseId(dto.getTargetId().longValue()).getData();
        Comment comment = new Comment();
        comment.setCommentText(dto.getCommentText());
        comment.setStarRank(dto.getStarRank());
        comment.setTargetId(dto.getTargetId().longValue());
        comment.setComeFrom("学成在线");
        comment.setStarRank(dto.getStarRank());
        comment.setUserName(user.getName());
        comment.setTargetName(courseVo.getName());
        comment.setTargetType("course");
        comment.setUserId(AuthInfoHolder.getUserId());
        comment.setCompanyId(courseVo.getCompanyId());
        comment.setBelongTo(1447452594738454500L);
        comment.setPraiseNum(1);
        comment.setNickName(RandomUtil.randomString(4));
        comment.setUserHead(user.getUserpic());
        comment.setCommentDate(new Date());
        //保存 查询其他表的数据
        this.save(comment);
        CommentVo commentVo = BeanHelper.copyProperties(comment, CommentVo.class);
        return ResponseResult.okResult(commentVo);
    }

    /**
     * 门户详情页----课程评论列表
     *
     * @param pageRequest
     * @param dto
     * @return
     */
    @Override
    public ResponseResult forCourse(PageRequest pageRequest, CommentQueryDto dto) {
        Page<Comment> page = new Page(pageRequest.getPageNo(), pageRequest.getPageSize());
        //组装条件
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        //设置条件
        qw.eq(dto.getLevel() != null, Comment::getStarRank, dto.getLevel())//评价等级
                .eq(!StringUtils.isEmpty(dto.getReplyStatus()), Comment::getReplyStatus, dto.getReplyStatus())//评价状态
                .eq(dto.getTargetId() != null, Comment::getTargetId, dto.getTargetId())
                .eq(!StringUtils.isEmpty(dto.getTargetName()), Comment::getTargetName, dto.getTargetName());
        Page<Comment> commentPage = this.page(page, qw);
        //循环设置课程回复表
        List<CommentVo> commentVos = BeanHelper.copyWithCollection(commentPage.getRecords(), CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            //查询评论回复表
            LambdaQueryWrapper<CommentReply> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(CommentReply::getCommentId, commentVo.getCommentId());
            List<CommentReply> commentReplyList = commentReplyService.list(qw2);
            List<CommentReplyVo> commentReplyVoList = BeanHelper.copyWithCollection(commentReplyList, CommentReplyVo.class);
            commentVo.setReplyDTOList(commentReplyVoList);
        }
        return PageResponseResult.okResult(commentPage.getTotal(), commentVos);
    }

    /**
     * 门户详情页----课程评论统计
     *
     * @param targetId
     * @return
     */
    @Override
    public ResponseResult CountTargetId(Long targetId) {
        //根据课程id查询课程的统计
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        qw.eq(Comment::getTargetId, targetId);
        //查询所有的评论课程的数
        List<Comment> commentList = this.list(qw);
        Integer total = 0;
        Integer starRank1Nums = 0;
        Integer starRank2Nums = 0;
        Integer starRank3Nums = 0;
        Integer starRank4Nums = 0;
        Integer starRank5Nums = 0;
        CourseCommentCountVo vo = new CourseCommentCountVo();
        for (Comment comment : commentList) {
            if (comment.getStarRank() == 1) {
                vo.setStarRank1Nums(starRank1Nums += 1);
            }
            if (comment.getStarRank() == 2) {
                vo.setStarRank2Nums(starRank2Nums += 1);
            }
            if (comment.getStarRank() == 3) {

                vo.setStarRank3Nums(starRank3Nums += 1);
            }
            if (comment.getStarRank() == 4) {
                vo.setStarRank4Nums(starRank4Nums += 1);

            }
            if (comment.getStarRank() == 5) {
                vo.setStarRank5Nums(starRank5Nums += 1);
            }
            vo.setTotalCommentNums(total += 1);
            vo.setTargetId(comment.getTargetId().intValue());

        }
        //平均分
        double sum = (starRank1Nums * 1 + starRank2Nums * 2 + starRank3Nums * 3 + starRank4Nums * 4 + starRank5Nums * 5) / total;
        vo.setStarRank(sum + "");
        return ResponseResult.okResult(vo);
    }
}
